﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Harkon.AppServer.cOne.Service
{
    [DataContract]
    public class ComponentInformation
    {
        [DataMember]
        public string Name { get; private set; }

        [DataMember]
        public bool Autostart { get; private set; }

        [DataMember]
        public string[] Dependencies { get; private set; }

        [DataMember]
        public string Uri { get; set; }

        public IDictionary<ServiceName, object> Services = new Dictionary<ServiceName, object>();

        public ComponentInformation(string name)
        {
            Name = name;
        }

        public ComponentInformation(string name, bool autostart, string[] dependencies)
            : this(name)
        {
            Autostart = autostart;
            Dependencies = dependencies;
        }

        public void AddService(ServiceName name, object target)
        {
            Services.Add(name, target);
        }

        public override string ToString()
        {
            if (Services != null)
                return string.Format("Component [{0} with {1} service{2}]", Name, Services.Count, Services.Count == 1 ? "" : "s");
            else
                return string.Format("Component [{0} with 0 services]", Name);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            ComponentInformation that = obj as ComponentInformation;

            return that.Name.Equals(Name) && that.Uri.Equals(Uri);
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    }

    [DataContract]
    public class RemoteComponentInformation : ComponentInformation
    {
        public RemoteComponentInformation(string name)
            : base(name)
        {

        }

        public RemoteComponentInformation(string name, bool autostart, string[] dependencies)
            : base(name, autostart, dependencies)
        {
        }

        public RemoteComponentInformation(ComponentInformation componentInfo)
            : base(componentInfo.Name)
        {
            Uri = componentInfo.Uri;
        }

        public override string ToString()
        {
            return string.Format("RemoteComponent [{0} on Uri={1}]", Name, Uri);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            RemoteComponentInformation that = obj as RemoteComponentInformation;

            return that.Name.Equals(Name) && that.Uri.Equals(Uri);
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    }
}
